package com.inossem.warehousestorage_inossem.activity.up_frame;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.up_frame.extra.UpFrameExtra;
import com.inossem.warehousestorage_inossem.adapter.up_frame.UpFrameListAdapter;
import com.inossem.warehousestorage_inossem.bean.up_frame.UpFrameManageBean;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.up_frame.UpFrameManageRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;

import java.util.List;

public class UpFrame_ManageActivity extends BaseActivity {

    private TextView title;

    private PullToRefreshLayout refresh;

    private PullableListView upFrameList;

    private UpFrameListAdapter adapter;

    private List<UpFrameManageBean.DataBean.UpFrameListBean> allData;

    private String mCondition;

    private boolean mFlag = false;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_up_frame_manage);
        initView(UpFrame_ManageActivity.this);
        initData(UpFrame_ManageActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        upFrameList = (PullableListView) activity.findViewById(R.id.upFrameList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.upFrameRefresh);
    }

    private void initData(Activity activity) {
        title.setText(R.string.activity_up_frame_manage_title);
        ActivityUtils.initBackAndFunction(activity, R.string.activity_main_title_short, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        getExtra(activity);
        if (mFlag) {
            Intent intent = new Intent(activity, UpFrame_ManageActivity.class);
            startActivity(intent);
            finish();
        } else {
            upFrameList.setPullUpFlag(false);
            refresh.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                    initData(activity);
                    pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                }

                @Override
                public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                }
            });
            mCondition = "";
            getData(activity);
//            setmScanListener((String scanResult, int length) -> {
//                mCondition = QRCodeUtils.DecryptQRCode(scanResult);
//                getData(activity);
//            }, QRCodeConstant.TYPE_BATCH, QRCodeConstant.TYPE_MATERIEL_CODE, QRCodeConstant.TYPE_PURCHASE_ORDER, QRCodeConstant.TYPE_POSITION);
        }
    }

    private void getData(Context context) {
        UpFrameManageRequest.request(context, mCondition, new GsonBeanListener<UpFrameManageBean>() {
            @Override
            public void onSuccess(UpFrameManageBean bean) {
                allData = bean.getData().getUpFrameList();
                if (allData.isEmpty()) {
                    Utils.middleToast(context, "未查到数据");
                }
                addUpFrameListData(context, ActivityUtils.getShowData(allData, 0), upFrameList, true);
            }
        });
    }

    private void addUpFrameListData(Context context, List<UpFrameManageBean.DataBean.UpFrameListBean> upFrameList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new UpFrameListAdapter(context, upFrameList, mCondition, (String condition) -> {
                mCondition = condition;
                getData(context);
            });
            listView.setAdapter(adapter);
        }
    }

    public interface UpFrameManageListener {
        void onNoDoubleClickListener(String condition);
    }

    private void getExtra(Activity activity) {
        mFlag = activity.getIntent().getBooleanExtra(UpFrameExtra.FLAG, false);
    }

}
